package org.an.demo.test;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Bundle;
import android.widget.ImageView;
import org.an.demo.R;
import org.an.ku.base.BaseActivity;

/**
 * 创建时间：2016年11月18日18:11 <br>
 * 作者：fuchaoyang <br>
 * 描述：https://github.com/wasabeef/picasso-transformations
 * 将drawable绘制成圆形
 */

public class BitmapTransformActivity extends BaseActivity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bitmap_trans);

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    imageView.setImageBitmap(transformLTRound(source, 50));
  }

  public Bitmap transformLTRound(Bitmap source, int radius) {
    int width = source.getWidth();
    int height = source.getHeight();
    int diameter = 2 * radius;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    // 先绘制左上角 RoundRect,再绘制这个图像下面和右边的部分
    canvas.drawRoundRect(new RectF(0, 0, diameter, diameter), radius, radius, paint);
    canvas.drawRect(new RectF(0, radius, radius, height), paint);
    canvas.drawRect(new RectF(radius, 0, width, height), paint);
    source.recycle();
    return bitmap;
  }

  public Bitmap transformCircle(Bitmap source, int radius) {
    // 创建一个和源文件尺寸相同的Bitmap
    int width = source.getWidth();
    int height = source.getHeight();
    int minWidth = Math.min(width, height);
    Bitmap bitmap = Bitmap.createBitmap(minWidth, minWidth, Bitmap.Config.ARGB_8888);
    // 为这个bitmap创建一个画布
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    //位图渲染器(参数1:我们要操作的Bitmap,参数2.3:X轴,Y轴的填充类型,
    // 类型一共有三种,REPEAT:重复类型,CLAMP:拉伸类型(注意这里的拉伸是指拉伸图片的而最后一个像素),MIRROM:镜像类型)
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    canvas.drawCircle(source.getWidth() / 2, source.getWidth() / 2, source.getWidth() / 2, paint);
    // 释放资源
    source.recycle();
    return bitmap;
  }

  /**
   * 如果是RoundRect scaleType不能设置为centerCrop
   */
  public Bitmap transformRound(Bitmap source, int radius) {
    int width = source.getWidth();
    int height = source.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    canvas.drawRoundRect(new RectF(0, 0, width, height), radius, radius, paint);
    source.recycle();
    return bitmap;
  }
}
